﻿using STMS.BLL;
using STMS.WSTMSystem.FModels;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.Store
{
    public partial class FrmStoreInfo : Form
    {
        public FrmStoreInfo()
        {
            InitializeComponent();
        }

        public event Action ReloadStoreList;//刷新仓库列表
        FInfoData fModel = null;
        int storeId = 0;//当前的仓库编号
        StoreBLL storeBLL = new StoreBLL();
        string oldName = "";

        private void FrmStoreInfo1_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                fModel = this.Tag as FInfoData;
                InitStoreInfo();
            }
        }

        private void InitStoreInfo()
        {
            if (fModel != null)
            {
                string pageTypeName = "新增";
                switch (fModel.ActType)
                {
                    case 1:
                        ClearInfo();
                        btnClear.Enabled = true;
                        pageTypeName = "新增";
                        break;
                    case 2:
                        storeId = fModel.FId;
                        LoadStore(storeId);
                        btnClear.Enabled = false;
                        pageTypeName = "修改";
                        break;
                    default: break;
                }
                this.Text += "----" + pageTypeName;
            }
        }

        /// <summary>
        /// 加载仓库信息
        /// </summary>
        /// <param name="storeId"></param>
        private void LoadStore(int storeId)
        {
            StoreInfo store = storeBLL.GetStore(storeId);
            if (store != null)
            {
                txtStoreNo.Text = store.StoreNo;
                txtStoreName.Text = store.StoreName;
                txtRemark.Text = store.Remark;
                oldName = store.StoreName;
            }
        }

        /// <summary>
        /// 清空信息
        /// </summary>
        private void ClearInfo()
        {
            txtRemark.Clear();
            txtStoreName.Clear();
            txtStoreNo.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string storeName = txtStoreName.Text.Trim();
            string storeNo = txtStoreNo.Text.Trim();
            string remark = txtRemark.Text.Trim();
            if (CheckStoreInfo(storeName) == false) return;
            StoreInfo storeInfo = new StoreInfo()
            {
                StoreId = storeId,
                StoreName = storeName,
                StoreNo = storeNo,
                Remark = remark
            };
            bool bl = false;
            if (fModel.ActType == 1)
            {
                int reNewId = storeBLL.AddStoreWithId(storeInfo);
                if (reNewId > 0)
                {
                    bl = true;
                    storeId = reNewId;
                    oldName = storeName;
                }
            }
            else
            {
                bl = storeBLL.UpdateStore(storeInfo);
            }
            string msgTitle = "仓库信息";
            string typeName = fModel.ActType == 1 ? "新增" : "修改";
            if (bl)
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"仓库:{storeName} {typeName} 成功！");
                if (fModel.ActType == 1)
                    fModel.ActType = 2;
                ReloadStoreList?.Invoke();
            }
            else
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"仓库:{storeName} {typeName} 失败！");
                return;
            }
        }

        /// <summary>
        /// 检查仓库信息
        /// </summary>
        /// <param name="storeName"></param>
        /// <returns></returns>
        private bool CheckStoreInfo(string storeName)
        {
            string msgTitle = "提交仓库信息";
            if (string.IsNullOrEmpty(storeName))
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "仓库名称不能为空！");
                return false;
            }
            if (oldName != "" && storeName != oldName || oldName == "")
            {
                if (storeBLL.ExistsStore(storeName))
                {
                    MsgBoxHelper.MsgErrorShow(msgTitle, "仓库名称已存在！");
                    return false;
                }
            }
            return true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearInfo();
            fModel.ActType = 1;
            storeId = 0;
            oldName = "";
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
